package com.atguigu.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseMapper;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.entity.AdminRole;
import com.atguigu.entity.Role;
import com.atguigu.mapper.AdminMapper;
import com.atguigu.mapper.AdminRoleMapper;
import com.atguigu.mapper.RoleMapper;
import com.atguigu.service.RoleService;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl extends BaseServiceImpl<Role> implements RoleService {
//    默认按照class进行装配
//    @Autowired
//默认按照类名进行装配
    @Resource
    private RoleMapper roleMapper;

    @Resource
    private AdminMapper adminMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }


    /**
     * 根据用户id查询用户的角色
     * @param adminId
     * @return
     */
    @Override
    public Map<String, Object> findRoleByAdminId(Long adminId) {
        //查询所有角色
        List<Role> allRolesList = roleMapper.findAll();
        //根据用户分配角色
        List<Long> existRoleIdList = adminRoleMapper.findRoleIdByAdminId(adminId);
        //已选择assginRoleList
        List<Role> assginRoleList = new ArrayList<>();
        //未选择noAssginRoleList
        List<Role> noAssginRoleList = new ArrayList<>();

        for (Role role : allRolesList) {
            if (existRoleIdList.contains(role.getId())) {
                assginRoleList.add(role);
            }else {
                noAssginRoleList.add(role);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("noAssginRoleList", noAssginRoleList);
        map.put("assginRoleList", assginRoleList);
        return map;
    }

    /**
     * 分配角色
     * @param adminId
     * @param roleIds
     */
    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] roleIds) {
        //删除用户所具有的角色
        adminRoleMapper.deleteByAdminId(adminId);
        for (Long roleId : roleIds) {
            if (StringUtils.isEmpty(roleId)) continue;
            AdminRole userRole = new AdminRole();
            userRole.setAdminId(adminId);
            userRole.setRoleId(roleId);
            adminRoleMapper.insert(userRole);
        }
    }

    @Override
    protected BaseMapper<Role> getEntityMapper() {
        return roleMapper;
    }

//    @Override
//    public Integer insert(Role role) {
//        return roleMapper.insert(role);
//    }

//    @Override
//    public Role getById(Long id) {
//        return roleMapper.getById(id);
//    }
//
//    @Override
//    public Integer update(Role role) {
//        return roleMapper.update(role);
//    }
//
//    @Override
//    public Role getById(Serializable id) {
//        return null;
//    }
//
//    @Override
//    public Integer delete(Long id) {
//        return roleMapper.delete(id);
//    }
//
//    @Override
//    public PageInfo<Role> findPage(Map<String, Object> filters) {
//        int pageNum = CastUtil.castInt(filters.get("pageNum"), 1);
//        int pageSize = CastUtil.castInt(filters.get("pageSize"), 10);
//        //开始分页
//        PageHelper.startPage(pageNum, pageSize);
//        return new PageInfo<Role>(roleMapper.findPage(filters),10);
//    }


}
